package org.component 
{
	import com.utils.slider.ListIceSlider;
	import flash.display.MovieClip;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import org.component.interfaces.IListItem;
	import org.component.item.ListItem;
	import org.component.interfaces.IList;
	
	/**
	 * ...
	 * @author ...
	 */
	
	[Event(name = "list_select", type = "org.component.events.ListEvent")]
	[Event(name = "list_select_room", type = "org.component.events.ListEvent")]
	
	
	public class List extends EventDispatcher implements IList
	{
		
		private var _classItem:Class = null;      
		
		
		private var _column:int = 1;
		
		private var _row:int = 1;
		
		private var _itemHeight:Number = 0;    
		
		private var _spaceHeight:Number = 0;
		
		private var _assest:MovieClip = null;
		
		private var _slider:Slider = null;
		
		private var _listView:MovieClip = null;
		
		private var _items:Array = null;
		
		private var _listDataArr:Array = null;
		
		
		
		public function List(ui:MovieClip = null,item:Class = null,column:int=1,row:int=1,itemHeight:Number = 0,spaceHeight:Number=1) 
		{
			_assest = ui;
			_classItem = item;
			_itemHeight = itemHeight;
			_spaceHeight = spaceHeight;
			_row = row;
			_column = column;
			initialization();
			configEventListeners();
		}
		
		/**
		 * 初始化
		 */
		protected function initialization():void
		{
			_slider = new Slider(_assest["slider"]);
			
			_listView = _assest["listView"];
			
			_listView.scrollRect = new Rectangle(0, 0, _listView.width+1, _listView.height - _itemHeight - _spaceHeight);
			
			_items = [];
			
			for (var i:int = 0; i < _column*(_row+1); i++)
			{
				var item:IListItem = new _classItem(_listView["item"+i]) as IListItem;
				item.getSubject(this);
				_items[_items.length] = item;
			}
		}
		/**
		 * 注册监听
		 */
		protected function configEventListeners():void
		{
			_slider.addEventListener(Event.CHANGE, onChange);
			
		}
		
		
		private function onChange(event:Event):void
		{
			var rect:Rectangle = _listView.scrollRect;
			rect.y =  _slider.value / 100 * (_itemHeight * _clen - _listView.scrollRect.height);
			
			index = Math.floor(rect.y / _itemHeight);
			
			_listView.scrollRect = rect;
			
			
		}
		
		private function setNewList(_line:int = 0):void
		{
			var j:int = 0;	
			var pos:Number = _itemHeight * _line;
			var p:int = -1;
			
			for (var i:int = _line ; i < _row+1 + _line ; i++)	
			{
				for (var x:int = 0; x < _column; x++)
				{
					var listItem:IListItem = _items[j + x];
					listItem.y = pos;
					p = i * _column + x;
					
					if (_list[p]!=null)
					{
						if (listItem.index != p) listItem.index = p;
						listItem.update(_list[p]);

					}else
					{
						listItem.dispose();
					}
				}
				pos += _itemHeight;
				j+=_column;
			}
		}
		
		private var _list:Array = null;
		
		private var _clen:int = -1;
		/**
		 * 
		 * @param	list
		 */
		public function update(list:Array = null):void
		{
			selectIndex = -1;
			selectItem = null;
			_list = list?list:[];
			_clen = Math.ceil(_list.length / _column);
			_slider.value = 0;
			_index = -1;
			_slider.update(_row, _clen);
			onChange(null);
			_listDataArr = list;
			
			
		}
		
		private var _selectItem:IListItem = null;
		public function get selectItem():IListItem { return _selectItem; }
		public function set selectItem(value:IListItem):void
		{
			if (_selectItem)
			{
				_selectItem.select = false;
			}
			_selectItem = value;
			if (_selectItem)
			{
				_selectItem.select = true;
			}
		}
		
		private var _selectIndex:int = -1;
		public function get selectIndex():int { return _selectIndex; }
		public function set selectIndex(value:int):void
		{
			_selectIndex = value;
		}
		
		private var _index:int = -1;
		public function get index():int { return _index; }
		public function set index(value:int):void
		{
			if (_index == value) return;
			
			_index = value;
			setNewList(index);
		}
		
		/**
		 * 
		 * @param	type 
		 * @param	value
		 * @return
		 */
		public function getTypeByItem(type:String = "", value:int = -1):ListItem
		{
			for each(var item:ListItem in _items)
			{
				if (item.listdata && item.listdata[type] == value) return item;
				
			}
			
			return null;
		}
		
		public function setSliderVisible(value:Boolean):void
		{
			_slider.setVisible(value);
		}
		
		public function get listDataArr():Array
		{
			return _listDataArr;
		}
		
		
		
	}

}